/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/3/7 16:01
 * @description:
 * @version:
 */

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author liuhuifang
 * @date 2022/5/26 11:31
 * CountDownLatch是一个同步工具类，它通过一个计数器来实现的,初始值为线程的数量。每当一个线程完成了自己的任务,计数器的值就相应得减1。
 * 当计数器到达0时,表示所有的线程都已执行完毕,然后在等待的线程就可以恢复执行任务。
 */
public class CountDownLaunchTest {

    public static void main(String[] args) throws InterruptedException {
        List list = new ArrayList();
       /* for(int i = 1; i<=100; i++){
            list.add(i);
        }
        Long start = System.currentTimeMillis();
        for(int i = 0; i<list.size(); i++){
            Thread.sleep(100);
        }
        System.out.println("=====同步执行：耗时"+(System.currentTimeMillis()-start)+"毫秒======");*/
/*
        Long start1 = System.currentTimeMillis();
        CountDownLatch latch = new CountDownLatch(10);
        //开启10个异步线程
        for(int i = 0; i<latch.getCount(); i++){
            new Thread(new Test(latch, i, list)).start();
        }
        latch.await();
        System.out.println("=====异步执行：耗时"+(System.currentTimeMillis()-start1)+"毫秒======");*/

        Long start2 = System.currentTimeMillis();
        CountDownLatch latch2 = new CountDownLatch(10);
        //开启10个异步线程
        for(int i = 0; i<latch2.getCount(); i++){
            new Thread(new Test2(i, list)).start();
            latch2.countDown();
        }
        latch2.await();
        System.out.println("=====异步执行：耗时"+(System.currentTimeMillis()-start2)+"毫秒======");

      /*  Long start2 = System.currentTimeMillis();
       CountDownLatch latch2 = new CountDownLatch(10);
       ExecutorService executor = Executors.newFixedThreadPool(10);
        for (int i = 0; i < latch2.getCount(); i++) {
            Runnable runnableTask = () -> {
                try {
                    for(int a = 0; a < 10; a++){
                        // 执行任务逻辑
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }catch (Exception e) {
                    System.out.println("程序异常");
                }finally {
                    latch2.countDown();
                }
            };
            executor.submit(runnableTask);
        }
        latch2.await();
        System.out.println("=====异步执行2：耗时"+(System.currentTimeMillis()-start2)+"毫秒======");
        executor.shutdown();*/
    }

    static class Test implements Runnable{
        private CountDownLatch latch;
        private int i;
        private List list;
        Test(CountDownLatch latch, int i, List list){
            this.latch = latch;
            this.i = i;
            this.list = list;
        }

        @Override
        public void run() {
            for(int a = i*10; a<(i+1)*10; a++){
                // 执行任务逻辑
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            latch.countDown();
        }
    }

    static class Test2 implements Runnable{
        private int i;
        private List list;
        Test2( int i, List list){
            this.i = i;
            this.list = list;
        }

        @Override
        public void run() {
            for(int a = i*10; a<(i+1)*10; a++){
                // 执行任务逻辑
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }



}

